Description:
It simplifies programming but does not make it possible for the compiler to
detect unhandled exceptions. ENH tries to detect such problems by
building a method call closure and propagating thrown exceptions.
ENH considers only exceptions explicitly generated by a program (using the
raise statement) and not runtime exceptions caused by system methods, such as
invalid array index or null reference exceptions.
Unfortunately, it is not clear when an exception has to be caught and handled,
so error messages are produced by ENH in two situations:
try-except statement (so the
try-except statement handles some other exception, but not this one).Incorrect:
type
NoSuchFileException = class(System.Exception)
end;
CorruptedFileException = class(System.Exception)
end;
VersionNotSupportedException = class(System.Exception)
end;
Database = class
private
const SupportedVersion: String = '3.0';
public
procedure Open(fileName:String);
function OpenFile(fileName:String):boolean;
function ReadHeader():boolean;
procedure set_Version(const ver: String);
function get_Version(): String;
property Version:String read get_Version write set_Version;
end;
MyApplication = class
public
class procedure Main(args: array of String);
end;
...
procedure Database.Open(fileName:String);
begin
if not OpenFile(fileName) then
raise NoSuchFileException.Create();
if not ReadHeader() then
raise CorruptedFileException.Create;
if Version < SupportedVersion then
raise VersionNotSupportedException.Create;
end;
class procedure MyApplication.Main(args: array of String);
var db:Database;
begin
db := Database.Create;
try
db.Open(args[0]);
except
on NoSuchFileException do
System.Console.WriteLine('File not found');
end;
end;
Correct:
class procedure MyApplication.Main(args: array of String);
var db:Database;
begin
db := Database.Create;
try
db.Open(args[0]);
except
on NoSuchFileException do
System.Console.WriteLine('File not found');
on CorruptedFileException do
System.Console.WriteLine('File is corrupted');
on VersionNotSupportedException do
System.Console.WriteLine('Version is not supported');
end;
end;